const Koa = require('koa');
const Router = require('koa-router');
const Koa_Logger = require("koa-logger");
const koaBody = require('koa-body')

const app = new Koa();
const router = new Router();
const logger = new Koa_Logger();

router.get("/baseurl", async (ctx)=>{
    ctx.body = "<h1>Node Koa</h1>"
});

router.get("/baseurl/path1", async (ctx)=>{
    ctx.redirect("/baseurl/path3");
});

router.get("/baseurl/path2", async (ctx)=>{
    ctx.body = "<h1>Node Koa Path2</h1>\n<span>1</span>"
});

router.post("/baseurl/path2", async (ctx)=>{
    let {a, b} = ctx.request.body;
    console.log(a,b)
    ctx.body = "{\"a\": 1}"
});

router.get("/baseurl/path3", async (ctx)=>{
    ctx.body = "<h1>Node Koa Path3</h1>"
});

app.use(logger);
app.use(koaBody());
app.use(router.routes());

app.listen(8881, () => {
    console.log('应用已经启动，http://localhost:8881');
});